/*
*/

#include "control.h"

#include "beam.h"
#include "display.h"
#include "event.h"
#include "form.h"
#include "keypad.h"
#include "rtc.h"
#include "test.h"
#include "timer.h"
#include "sensor.h"

typedef void(*PRESS)(u32);

static void	KeyBak	(u32);
static void	KeyChM	(u32);
static void	KeyChP	(u32);
static void	KeyNum	(u32);
static void	KeyPag	(u32);
static void	KeyPro	(u32);
static void	KeyReg	(u32);
static void	KeyRep	(u32);
static void	KeySar	(u32);
static void	KeyTst	(u32);

static u32	ch_key=0;
static u32	ch_int=1;
static u32	ch_max=300;
static u32	ch_min=1;
static u16	devices[4]={DEV_PANA,DEV_TCL,DEV_2,DEV_3};
static u32	t_beam,t_prog,t_search,t_test;

static void KeyBak(u32 e){
    KeyChP(e);
}

static void KeyChM(u32 e){
    KeyRep(KEY_CHLD);
    ch_int--;
    if(ch_int<ch_min)
	ch_int=ch_max;
    if(ch_int>ch_max)
	ch_int=ch_max;
}

static void KeyChP(u32 e){
    KeyRep(KEY_CHLI);
    ch_int++;
    if(ch_int<ch_min)
	ch_int=ch_min;
    if(ch_int>ch_max)
	ch_int=ch_min;
}

static void KeyNum(u32 key){
    KeyReg(key);
    if(key>9)
	key=0;
    ch_key=ch_key*10+key;
    if(ch_key>999)
	ch_key%=1000;
    ch_int=ch_key;
}

static void KeyPag(u32 key){
    t_search=Timer();
    Page(key-KEY___TV);
    KeyReg(KEY_TVAV);
}

static void KeyPro(u32 e){
    t_prog=Timer();
    KeyReg(e);
}

static void KeyReg(u32 key){
    BeamHead(key);
    IndicatorOn();
}

static void KeyRep(u32 key){
    t_beam=BeamHead(key);
    IndicatorOn();
}

static void KeySar(u32 e){
    KeyChM(e);
}

static void KeyTst(u32 key){
    t_test=Timer();
    KeyReg(key);
    switch(key){
    case KEY_LANG:
    case KEY_ZOOM:
	test=TestRTC;
	break;
    default:
	test=TestSensor;
    }
}

//------------------------------------------------------------------------------

void Control(void){
    const PRESS prs[KEY_MAX]={
	KeyReg,KeyNum,KeyNum,KeyNum,KeyNum,KeyNum,KeyNum,KeyNum,
	KeyNum,KeyNum,KeyNum,KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,
	KeyChP,KeyChM,KeyRep,KeyRep,KeyReg,KeyReg,KeyReg,KeyReg,
	KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,
	KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,KeyTst,
	KeyTst,KeyTst,KeyTst,KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,
	KeyReg,KeyReg,KeyPag,KeyPag,KeyPag,KeyPag,KeyPag,KeyPag,
	KeyReg,KeyReg,KeyReg,KeyReg,KeyReg,KeyBak,KeySar,KeyPro
    };
    u32	e;

    while(status==STT_CONTROL){
	e=WaitEvent();
	switch(e&EVN_MASK){
	case EVN_KEYPRESS:
	    t_indicate=0;
	    e=GetKey(e);
	    prs[e](e);
	    break;
	case EVN_KEYRELEASE:
	    BeamOff();
	    t_beam=
	    t_prog=
	    t_search=
	    t_test=0;
	    t_indicate=Timer();
	    break;
	}
	if(TimeOut(t_beam,beam.timeout))
	    t_beam=BeamTail();
	else if(TimeOut(t_prog,3000)){
	    status=STT_LEARNING;
	    BeamOff();
	    t_prog=0;
	}
	else if(TimeOut(t_search,3000)){
	    status=STT_SEARCH;
	    BeamOff();
	    t_search=0;
	}
	else if(TimeOut(t_test,3000)){
	    status=STT_TEST;
	    t_test=0;
	}
	if(TimeOut(t_indicate,1000)){
	    ch_key=0;
	    t_indicate=0;
	    IndicatorOff();
	}
    }
}

void Page(u32 pag){
    page=pag;
    Load(devices[page],keywave,64,0);
}
